import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_audioplayers.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/hazard_investigation/hazard_management/hazard_rectification/hazard_rectification_controller.dart';
import 'package:url_launcher/url_launcher.dart';

/// 我的任务-隐患排查-隐患治理-隐患整改、隐患验收页面
class HazardRectificationPage extends StatelessWidget {
  HazardRectificationPage({super.key});

  final controller = Get.put(HazardRectificationController());

  /// 获取展开按钮
  Widget _getExpandButton() {
    return Obx(() => Row(
          children: [
            Text(
              controller.isExpanded.value ? '收起' : '展开',
              style: TextStyle(
                fontSize: 14.sp,
                color: Colors.white,
                fontWeight: FontWeight.w400,
              ),
            ),
            AppGaps.hGap6,
            SvgPicture.asset(
              controller.isExpanded.value ? AppImages.icon_circle_up : AppImages.icon_circle_down,
              width: 16.w,
              height: 16.w,
            )
          ],
        ).clickOnce(controller.onExpanded));
  }

  /// 获取基础信息
  Widget _getBasicInformation() {
    return Container(
        width: AppDimens.sh,
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.w),
        color: const Color(0xFF5C6DFF),
        child: Column(
          children: [
            Row(
              children: [
                SvgPicture.asset(
                  AppImages.icon_title_task,
                  width: 17.w,
                  height: 17.w,
                ),
                AppGaps.hGap6,
                Expanded(
                    child: Text(
                  controller.infoEntity.troubleshootContent!,
                  style: TextStyle(
                    fontSize: 16.sp,
                    color: Colors.white,
                    overflow: TextOverflow.ellipsis,
                    fontWeight: FontWeight.w500,
                  ),
                )),
              ],
            ),
            AppGaps.vGap12,
            Row(
              children: [
                Expanded(
                    child: Text(
                  '隐患描述：${controller.infoEntity.dangerRecordDesc}',
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: Colors.white,
                    overflow: TextOverflow.ellipsis,
                    fontWeight: FontWeight.w400,
                  ),
                )),
                Container(
                  height: 14.w,
                  width: 2.w,
                  margin: EdgeInsets.only(right: 20.w, left: 10.w),
                  decoration: const BoxDecoration(
                      gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
                    Color(0x00ffffff),
                    Color(0xffffffff),
                    Color(0x00ffffff),
                  ])),
                ),
                _getExpandButton()
              ],
            )
          ],
        ));
  }

  /// 字段显示
  Widget _getFieldView(String title, String value, {Color? rightColor}) {
    return Expanded(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              color: const Color(0xFFA5A6B5),
              fontWeight: FontWeight.w400,
            ),
          ),
          Expanded(
              child: Text(
            value,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              color: rightColor ?? const Color(0xFF495061),
              fontWeight: FontWeight.w400,
            ),
          ))
        ],
      ),
    );
  }

  /// 获取详情视图
  Widget _getDetailsView() {
    return Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '拍照:',
            style: TextStyle(
              fontSize: 13.sp,
              color: const Color(0xffA5A6B5),
              overflow: TextOverflow.ellipsis,
              fontWeight: FontWeight.w400,
            ),
          ),
          AppGaps.vGap6,
          Wrap(
              spacing: 5.w,
              children: controller.infoEntity.dangerImages!.map<Widget>((item) {
                return Container(
                  width: 74.w,
                  height: 74.w,
                  margin: EdgeInsets.only(bottom: 10.w),
                  decoration: BoxDecoration(
                    color: const Color(0xffF7F7FB),
                    border: Border.all(
                      color: const Color(0xffEBEDF0),
                    ),
                    borderRadius: BorderRadius.circular(8.w),
                    image: DecorationImage(
                      image: NetworkImage(Interface.fileUrl + item),
                      fit: BoxFit.cover,
                    ),
                  ),
                ).clickShowLarge(url: Interface.fileUrl + item);
              }).toList()),
          AppGaps.vGap12,
          Text(
            '视频:',
            style: TextStyle(
              fontSize: 13.sp,
              color: const Color(0xffA5A6B5),
              overflow: TextOverflow.ellipsis,
              fontWeight: FontWeight.w400,
            ),
          ),
          AppGaps.vGap6,
          Wrap(
              spacing: 5.w,
              children: controller.infoEntity.dangerVideo!.map<Widget>((item) {
                return Container(
                  width: 160.w,
                  height: 90.w,
                  margin: const EdgeInsets.only(bottom: 10),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: const Color(0xff060B15),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: IconButton(
                    icon: const Icon(
                      Icons.play_circle,
                      color: Colors.white,
                      size: 30,
                    ),
                    onPressed: () {
                      FileUtils.openFile(Interface.fileUrl + item);
                    },
                  ),
                );
              }).toList()),
          AppGaps.vGap12,
          Text(
            '语音:',
            style: TextStyle(
              fontSize: 13.sp,
              color: const Color(0xffA5A6B5),
              overflow: TextOverflow.ellipsis,
              fontWeight: FontWeight.w400,
            ),
          ),
          AppGaps.vGap6,
          controller.infoEntity.dangerVoice!.isNotEmpty
              ? CustomAudioplayers(
                  audioUrl: controller.infoEntity.dangerVoice!,
                  isEnabled: false,
                )
              : Container(),
          AppGaps.vGap12,
          Row(
            children: [
              _getFieldView('隐患上报人：', controller.infoEntity.registrant!),
              _getFieldView('电话：', controller.infoEntity.registrantUserPhone!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('隐患确认人：', controller.infoEntity.confirmBy!),
              _getFieldView('电话：', controller.infoEntity.confirmUserPhone!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('隐患整改人：', controller.infoEntity.liablePerson!),
              _getFieldView('电话：', controller.infoEntity.liableUserPhone!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('隐患验收人：', controller.infoEntity.checkAcceptPerson!),
              _getFieldView('电话：', controller.infoEntity.checkAcceptUserPhone!),
            ],
          ),
          Obx(() => Visibility(
              visible: controller.isExpanded.value,
              child: Column(
                children: [
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险分析对象：', controller.infoEntity.riskObjectName!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('责任部门：', controller.infoEntity.hazardDep!),
                      _getFieldView('责任人：', controller.infoEntity.hazardLiablePerson!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险分析单元：', controller.infoEntity.riskUnitName!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险事件：', controller.infoEntity.riskEventName!),
                      _getCurrentRiskLevel(),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('风险描述：', controller.infoEntity.riskDescription!),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getFieldView('初始风险后果：', '${controller.infoEntity.initialRiskConsequences}'),
                      _getFieldView('初始风险度：', '${controller.infoEntity.initialRiskDegree}'),
                    ],
                  ),
                  AppGaps.vGap10,
                  Row(
                    children: [
                      _getInitialRiskLevel(),
                      _getFieldView('管控措施：', '${controller.infoEntity.controlMeasures}'),
                    ],
                  ),
                ],
              ))),
        ],
      ),
    );
  }

  /// 当前风险等级
  Widget _getCurrentRiskLevel() {
    String name = '';
    Color? rightColor;
    switch (controller.infoEntity.currentRiskLevel) {
      case '1':
        name = '重大风险';
        rightColor = const Color(0xffFD6B6B);
        break;
      case '2':
        name = '较大风险';
        rightColor = const Color(0xffFD6B6B).withOpacity(0.7);
        break;
      case '3':
        name = '一般风险';
        rightColor = const Color(0xffFFCE24);
        break;
      case '4':
        name = '低风险';
        rightColor = const Color(0xFF5C6DFF);
        break;
      default:
        break;
    }
    return _getFieldView('当前风险等级：', name, rightColor: rightColor);
  }

  /// 初始风险等级
  Widget _getInitialRiskLevel() {
    String name = '';
    Color? rightColor;
    switch (controller.infoEntity.initialRiskLevel) {
      case '1':
        name = '重大风险';
        rightColor = const Color(0xffFD6B6B);
        break;
      case '2':
        name = '较大风险';
        rightColor = const Color(0xffFD6B6B).withOpacity(0.7);
        break;
      case '3':
        name = '一般风险';
        rightColor = const Color(0xffFFCE24);
        break;
      case '4':
        name = '低风险';
        rightColor = const Color(0xFF5C6DFF);
        break;
      default:
        break;
    }
    return _getFieldView('初始风险等级：', name, rightColor: rightColor);
  }

  /// 初始隐患等级
  Widget _getHiddenDangerLevel() {
    Color? rightColor;
    switch (controller.infoEntity.dangerLevel) {
      case '重大隐患':
        rightColor = const Color(0xffFD6B6B);
        break;
      default:
        rightColor = const Color(0xffFFCE24);
        break;
    }
    return _getFieldView('隐患等级：', controller.infoEntity.dangerLevel!, rightColor: rightColor);
  }

  /// 获取隐患确认详情
  Widget _getConfirmDetailsView() {
    return Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          Row(
            children: [
              Container(
                width: 2.w,
                height: 12.w,
                color: const Color(0xff5C6DFF),
              ),
              AppGaps.hGap4,
              Expanded(
                child: Text(
                  '隐患名称：${controller.infoEntity.dangerName}',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: const Color(0xff262F38),
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ],
          ),
          AppGaps.vGap14,
          Row(
            children: [
              _getHiddenDangerLevel(),
              // _getFieldView('电话：', '15236524584'),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('隐患类型：', controller.infoEntity.hazardDangerType!),
              _getFieldView('治理类型：', controller.infoEntity.dangerManageType!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('隐患描述：', controller.infoEntity.dangerDesc!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('原因分析：', controller.infoEntity.dangerReason!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('控制措施：', controller.infoEntity.riskMeasureDesc!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('资金：', controller.infoEntity.cost!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('隐患整改期限：', controller.infoEntity.dangerManageDeadline!),
            ],
          ),
          AppGaps.vGap10,
          Row(
            children: [
              _getFieldView('整改责任人：', controller.infoEntity.liablePerson!),
              _getFieldView('验收人：', controller.infoEntity.checkAcceptPerson!),
            ],
          ),
        ],
      ),
    );
  }

  /// 获取隐患整改详情
  Widget _getAcceptanceView() {
    return Container(
      margin: EdgeInsets.only(top: 20.w),
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(6.w),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                width: 2.w,
                height: 12.w,
                color: const Color(0xff5C6DFF),
              ),
              AppGaps.hGap4,
              Expanded(
                child: Text(
                  '整改说明：${controller.infoEntity.liableDesc}',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: const Color(0xff262F38),
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ],
          ),
          AppGaps.vGap14,
          Text(
            '拍照:',
            style: TextStyle(
              fontSize: 13.sp,
              color: const Color(0xffA5A6B5),
              overflow: TextOverflow.ellipsis,
              fontWeight: FontWeight.w400,
            ),
          ),
          AppGaps.vGap6,
          Wrap(
              spacing: 5.w,
              children: controller.infoEntity.liableImages!.map<Widget>((item) {
                return Container(
                  width: 74.w,
                  height: 74.w,
                  margin: EdgeInsets.only(bottom: 10.w),
                  decoration: BoxDecoration(
                    color: const Color(0xffF7F7FB),
                    border: Border.all(
                      color: const Color(0xffEBEDF0),
                    ),
                    borderRadius: BorderRadius.circular(8.w),
                    image: DecorationImage(
                      image: NetworkImage(Interface.fileUrl + item),
                      fit: BoxFit.cover,
                    ),
                  ),
                ).clickShowLarge(url: Interface.fileUrl + item);
              }).toList()),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
              appBar: CustomAppBar(
                title: Text(
                  controller.isAcceptance ? '隐患验收' : '隐患整改',
                  style: TextStyle(
                    fontSize: 18.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w400,
                  ),
                ),
                leading: const BackButton(
                  color: Colors.white,
                ),
                iconButtonColor: Colors.white,
                backgroundColor: const Color(0xFF5C6DFF),
              ),
              body: Container(
                  color: const Color(0xFFF5F5F6),
                  child: Stack(
                    children: [
                      Container(
                        height: 144.w,
                        width: AppDimens.sh,
                        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.w),
                        color: const Color(0xFF5C6DFF),
                      ),
                      Container(
                        alignment: Alignment.topCenter,
                        child: Column(
                          children: [
                            // 基础信息
                            _getBasicInformation(),
                            Expanded(
                              child: SingleChildScrollView(
                                child: Container(
                                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                                  child: Column(
                                    children: [
                                      // 隐患数据
                                      _getDetailsView(),
                                      AppGaps.vGap20,
                                      // 隐患确认详情
                                      _getConfirmDetailsView(),
                                      // 整改说明
                                      Visibility(
                                        visible: controller.isAcceptance,
                                        child: _getAcceptanceView(),
                                      ),
                                      SizedBox(height: 80.w)
                                    ],
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      )
                    ],
                  )),
              bottomSheet: Container(
                height: 60.w,
                alignment: Alignment.center,
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: const Color(0xffBCBCC5).withOpacity(0.3), // 阴影颜色
                      offset: const Offset(0, -5), // 阴影的偏移量，向上偏移，即上方阴影
                      blurRadius: 10.0, // 阴影的模糊半径
                      spreadRadius: 2.0, // 阴影的扩散半径
                    ),
                  ],
                ),
                child: Row(
                  children: [
                    Visibility(
                      visible: controller.isAcceptance,
                      child: Container(
                          height: 40.w,
                          width: 120.w,
                          margin: EdgeInsets.only(right: 20.w),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              color: const Color(0xffffffff),
                              border: Border.all(color: const Color(0xffFD6B6B), width: 1.w),
                              borderRadius: BorderRadius.all(Radius.circular(20.w))),
                          child: Text(
                            '驳回',
                            style: TextStyle(
                              fontSize: 16.sp,
                              color: const Color(0xffFD6B6B),
                            ),
                          )).clickOnce(controller.reject),
                    ),
                    Expanded(
                      child: Container(
                          height: 40.w,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              color: const Color(0xff5C6DFF),
                              borderRadius: BorderRadius.all(
                                Radius.circular(20.w),
                              )),
                          child: Text(
                            controller.isAcceptance ? '通过' : '隐患整改',
                            style: TextStyle(
                              fontSize: 16.sp,
                              color: const Color(0xffffffff),
                            ),
                          )),
                    ),
                  ],
                ).clickOnce(controller.complete),
              ));
        });
  }
}
